package com.fenshi.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.android.volley.VolleyError;
import com.fenshi.bean.OrderBean;
import com.fenshi.interactor.BackCarInteractor;
import com.fenshi.interactor.DamageOrderInteractor;
import com.fenshi.interactor.imp.BackCarInteractorImp;
import com.fenshi.interactor.imp.DamageOrderInteractorImp;
import com.fenshi.manager.activity.backcar.BackActivity;
import com.fenshi.manager.activity.order.DamageOrderActivty;
import com.fenshi.presenter.BackCarPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.MethodType;
import com.fenshi.view.BackView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.LogHelper;

/**
 * Created by lilongfei on 15/10/13.
 */
public class BackCarPresenterImp implements BackCarPresenter ,RequestListener{

    BackView view;
    LogHelper log;
    Context c;
    BackCarInteractor interactor;
    DamageOrderInteractor interactorOrder;
    String cardId;
    OrderBean result;
    String status; // 订单状态
    /**
     * 验车
     * type 1 有定损， 0 无定损
     */

    public BackCarPresenterImp(Context c){
        this(c, ((BackView) c));
    }

    public BackCarPresenterImp(Context c, BackView view){
        this.c= c;
        this.view = view;
        interactor = new BackCarInteractorImp(c,this);
        interactorOrder = new DamageOrderInteractorImp(c, this);
        log = LogHelper.getInstance();
    }


    @Override
    public void initViewAndEvent() {
        view.initViewAndEvnet();
        Bundle bundle = ((Activity) c).getIntent().getExtras();
        if(bundle!=null){
            status = bundle.getString(CookicUntil.ORDER_STATUS);
            backCar();
        }
    }

    @Override
    public void backCar() {
        view.showLoad();
        if (((Activity) c).getIntent().getExtras()!=null) {
            cardId = ((Activity) c).getIntent().getExtras().getString(CookicUntil.CODE);
            String userId = CookicUntil.getUser().getUserId();
            if (userId == null || cardId == null){
                view.showError("参数异常");
                return;
            }

            interactor.backCar(userId,cardId);
        }else
            view.showError("卡片id传输失败");
    }

    public void eum(String picUrl,String dec){
        String userId = CookicUntil.getUser().getUserId();
        interactorOrder.evidence(result.getOrderCode(),userId,picUrl,dec);
    }

    @Override
    public void checkCar(int type) {
        view.showLoad();
        if (((Activity) c).getIntent().getExtras()!=null) {
            cardId = ((Activity) c).getIntent().getExtras().getString(CookicUntil.CODE);
            String userId = CookicUntil.getUser().getUserId();
            if (userId == null || cardId == null){
                view.showError("参数异常");
                return;
            }
            if (result == null){
                view.showError("还车订单生成出现异常,请联系管理员或重复步骤");
                return;
            }
            interactor.checkCar(userId, String.valueOf(type), result.getOrderCode());
        }
    }

    @Override
    public void upPic() {
        Intent intent = new Intent(c, DamageOrderActivty.class);
        Bundle bundle = new Bundle();
        bundle.putString(CookicUntil.ORDER_ID,result.getOrderCode());
        intent.putExtras(bundle);
        ((Activity) c).startActivityForResult(intent, BackActivity.BACKCAR);
    }

    @Override
    public void onActivityResult(int requestCode, int responseCode, Intent data) {

    }

    @Override
    public void Success(String tag, Object result) {
        view.dismissLoad();
        if(tag.equals(MethodType.CLOSE_THE_CAR)){
            if (result !=null){
                this.result = (OrderBean) result;
                view.refreshView((OrderBean) result);
                view.showError("车辆已经锁定");
            }else {
                view.showError("车辆锁定失败,请重试");
                ((Activity) c).finish();
            }
        }else if(tag.equals(MethodType.VAILE_CAR)){
            view.showError("验车完毕");
            if (Build.VERSION.SDK_INT >15)
                ((BaseActivity)c).finishAffinity();
            else
                ((BaseActivity)c).finish();
        }else if (tag.equals(MethodType.EVIDENE)){
            view.setEumShow(View.VISIBLE);
        }

    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        if(tag.equals(MethodType.CLOSE_THE_CAR))
        {
            view.showError("车辆锁定失败,请重试");
            ((Activity) c).finish();
        }else
            view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError("服务器异常，请稍后重试");
    }
}
